import uuid
from dataclasses import dataclass
from injector import inject
from model import App
from pkg.sqlalchemy import SQLAlchemy


@inject
@dataclass
class AppService:
    db: SQLAlchemy

    def create_app(self):
        app = App(name="机器人", account_id=uuid.uuid4(), icon='', description="desc")
        with self.db.auto_commit():
            self.db.session.add(app)
        return app

    def get_app(self, id):
        app = self.db.session.query(App).get(id)
        return app

    def update_app(self, id, data):
        app = self.db.session.query(App).get(id)
        for key, value in data.items():
            setattr(app, key, value)
            # self.db.session.commit()
        return app

    def delete_app(self, id):
        app = self.db.session.query(App).get(id)
        self.db.session.delete(app)
        # self.db.session.commit()
        return app
